package org.yudidi.properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 对比 {@link FooProperties}
 *
 * 事实上，{@link EnableConfigurationProperties} 已经自动将 Environment
 *
 * 中的配置应用到了被 {@link ConfigurationProperties} 注解的"bean"上
 *
 * 所以该类的配置等同于 {@link FooProperties} 和 {@link TestPropertiesConfiguration} 的组合配置
 */
@Component
@ConfigurationProperties(prefix = "foo")
public class BarProperties {
    private static final Logger logger = LoggerFactory.getLogger(BarProperties.class);
    /**
     * 这个注解在这里没实际用处，就是为了方便在该类构造完成后打印日志，看看配置信息是否加载到配置类中了
     */
    @PostConstruct
    public void verifyConfigurationProperties() {
        logger.error("构造函数执行后: BarProperties: {}", toString());
    }

    private boolean enabled;

    private InetAddress remoteAddress;

    private Security security;

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public InetAddress getRemoteAddress() {
        return remoteAddress;
    }

    public void setRemoteAddress(InetAddress remoteAddress) {
        this.remoteAddress = remoteAddress;
    }

    public Security getSecurity() {
        return security;
    }

    public void setSecurity(Security security) {
        this.security = security;
    }

    public static class Security {

        private String username;

        private String password;

        private List<String> roles = new ArrayList<>(Collections.singleton("USER"));

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public List<String> getRoles() {
            return roles;
        }

        public void setRoles(List<String> roles) {
            this.roles = roles;
        }

        @Override
        public String toString() {
            return "{ " + "username='" + username + '\'' + ", password='" + password + '\'' + " }";
        }
    }

    @Override
    public String toString() {
        return "{ " + "enabled=" + enabled + ", remoteAddress=" + remoteAddress + ", security="
                + security + " }";
    }
}